package net.ropelato.compactcarrace.player;

import java.io.File;
import java.util.Iterator;

import javax.media.j3d.Locale;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import net.ropelato.compactcarrace.graphics3d.Model;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.sun.j3d.utils.picking.PickIntersection;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;

public class Player
{
    Model model = null;
    
    float positionX = -0f;
    float positionY = 0f;
    float positionZ = -0f;

    float rotationX = 0f;
    float rotationY = 0f;
    float rotationZ = 0f;
    
    PickTool picker = null;

    public Player(String fileName, Locale locale)
    {
        
        try
        {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new File(fileName));

            Element root = doc.getRootElement();
            Iterator it = root.getDescendants();
            while (it.hasNext())
            {
                Object obj = it.next();
                if (obj instanceof Element)
                {
                    Element element = (Element) obj;

                    if (element.getName().equals("models"))
                    {
                        parseModel(element);
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        picker = new PickTool(locale);
    }

    private void parseModel(Element element)
    {
        Element modelElement = element.getChild("model");
        setup(new Model(modelElement.getAttributeValue("fileName")));
    }

    private void setup(Model model)
    {
        this.model = model;
        model.setScale(0.3f);
        model.setPickable(false);
    }
    
    public void setPositionX(float positionX)
    {
        this.positionX = positionX;
    }

    public void setPositionY(float positionY)
    {
        this.positionY = positionY;
    }

    public void setPositionZ(float positionZ)
    {
        this.positionZ = positionZ;
    }

    public void setPosition(float positionX, float positionY, float positionZ)
    {
        this.positionX = positionX;
        this.positionY = positionY;
        this.positionZ = positionZ;
    }

    public float getPositionX()
    {
        return model.getPositionX();
    }

    public float getPositionY()
    {
        return model.getPositionY();
    }

    public float getPositionZ()
    {
        return model.getPositionZ();
    }

    public void setRotationX(float rotationX)
    {
        setRotation(rotationX, rotationY, rotationZ);
    }

    public void setRotationY(float rotationY)
    {
        setRotation(rotationX, rotationY, rotationZ);
    }

    public void setRotationZ(float rotationZ)
    {
        setRotation(rotationX, rotationY, rotationZ);
    }

    public void setRotation(float rotationX, float rotationY, float rotationZ)
    {
        this.rotationX = rotationX;
        this.rotationY = rotationY;
        this.rotationZ = rotationZ;
    }

    public float getRotationX()
    {
        return model.getRotationX();
    }

    public float getRotationY()
    {
        return model.getRotationY();
    }

    public float getRotationZ()
    {
        return model.getRotationZ();
    }

    public void update()
    {
        updatePhysics();
    }

    public void updatePhysics()
    {
        picker.setShapeRay(new Point3d(positionX, positionY+3d, positionZ), new Vector3d(0.0, -50.0, 0.0));
        
        PickResult picked = picker.pickClosest();
        if(picked!=null && picked.numIntersections() > 0)
        {
            PickIntersection intersect = picked.getIntersection(0);
            Point3d nextpoint = intersect.getPointCoordinates();
            
            float intersectionPosY = (float)nextpoint.getY();
            //System.out.println("Intersection Y: "+(positionY+30d));
            //System.out.println(picked.numIntersections());

            //positionY = intersectionPosY;
            setPositionY(intersectionPosY);
        }
        
        model.setRotation(rotationX, rotationY, rotationZ);
        model.setPosition(positionX, positionY, positionZ);
        model.update();
       
    }

    public Model getModel()
    {
        return model;
    }

    public void setModel(Model model)
    {
        this.model = model;
    }

    public void turnY(float turnY)
    {
        rotationY += turnY;
    }

    public void move(float speed)
    {
        positionX -= (float) Math.sin(Math.toRadians(rotationY)) * speed;
        positionZ -= (float) Math.cos(Math.toRadians(rotationY)) * speed;
    }
}
